// Run Portfolio Weight Regressions
use ../dta/mf_hold, clear 
merge m:1 code date2 using ../dta/ranking_rolling
drop if _m==2
drop _m 

merge m:1 code date2 using ../src/monthly_stock_chars
drop if _m!=3 
drop _m 

gen     nmon_since_pvin = date2 - ym(2010,02) if date2<=ym(2011,11)
replace nmon_since_pvin = date2 - ym(2011,11) if date2>ym(2011,11) & date2<=ym(2013,01)
replace nmon_since_pvin = date2 - ym(2013,01) if date2>ym(2013,01) & date2<=ym(2013,09)
replace nmon_since_pvin = date2 - ym(2013,09) if date2>ym(2013,09) & date2<=ym(2014,09)
replace nmon_since_pvin = date2 - ym(2014,09) if date2>ym(2014,09) & date2<=ym(2015,06)

gen top150 = 0 
replace top150 = 1 if rank_t<=150 & rank_t!=.


local control "p_float_cap p_turnover_float p_ret p_ret_12 p_bm"
foreach x of varlist `control' {
	drop if `x'==.
}

gen ii = ln(1+indic) 
replace ii = 0 if marginable==1  


* columns (1) to (3) of Table 4
preserve

	eststo clear
	keep if date2>=ym(2011,12) & date2<=ym(2014,6) /* v1-3 */

	eststo: qui reghdfe proportion c.q_rank_t_5##c.nmon_since_pvin p_float_cap p_turnover_float p_ret p_ret_12 p_bm c.nmon_since_pvin#c.p_float_cap c.nmon_since_pvin#c.p_bm c.nmon_since_pvin#c.p_turnover_float c.nmon_since_pvin#c.p_ret c.nmon_since_pvin#c.p_ret_12, absorb(fundid code date2) cluster(fundid)

	eststo: qui reghdfe proportion c.top150##c.nmon_since_pvin p_float_cap p_turnover_float p_ret p_ret_12 p_bm c.nmon_since_pvin#c.p_float_cap c.nmon_since_pvin#c.p_bm c.nmon_since_pvin#c.p_turnover_float c.nmon_since_pvin#c.p_ret c.nmon_since_pvin#c.p_ret_12, absorb(fundid code date2) cluster(fundid)

	eststo: qui reghdfe proportion c.ii##c.nmon_since_pvin p_float_cap p_turnover_float p_ret p_ret_12 p_bm c.nmon_since_pvin#c.p_float_cap c.nmon_since_pvin#c.p_bm c.nmon_since_pvin#c.p_turnover_float c.nmon_since_pvin#c.p_ret c.nmon_since_pvin#c.p_ret_12, absorb(fundid code date2) cluster(fundid)

restore


preserve

	use "../src/0th_shsz.dta", clear
	gen marginable_month = ym(2010,2)
	gen vintage = 0
	tempfile v0_2010_2
	save `v0_2010_2'

	use "../src/1st_shsz.dta", clear
	merge 1:1 code using "../src/0th_shsz.dta"
	keep if _merge == 1 
	drop _merge
	gen marginable_month = ym(2011,11)
	gen vintage = 1
	tempfile v1_2011_11
	save `v1_2011_11'

	use "../src/2nd_shsz.dta", clear
	merge 1:1 code using "../src/1st_shsz.dta"
	keep if _merge == 1 
	drop _merge
	gen marginable_month = ym(2013,1)
	gen vintage = 2
	tempfile v2_2013_1
	save `v2_2013_1'

	use "../src/3rd_shsz.dta", clear
	merge 1:1 code using "../src/2nd_shsz.dta"
	keep if _merge == 1 
	drop _merge
	gen marginable_month = ym(2013,9)
	gen vintage = 3
	tempfile v3_2013_9
	save `v3_2013_9'

	use "../src/4th_shsz.dta", clear
	merge 1:1 code using "../src/3rd_shsz.dta"
	keep if _merge == 1
	drop _merge 
	gen vintage = 4
	gen marginable_month = ym(2014,9)

	append using `v3_2013_9'
	append using `v2_2013_1'
	append using `v1_2011_11'
	append using `v0_2010_2'

	sort code marginable_month
	by code: keep if _n==1
	tempfile temp
	save `temp'

restore
	
merge m:1 code using `temp'
drop if _m==2 
drop _merge 

cap drop marginable
gen marginable = 0 
replace marginable = 1 if date2>=marginable_month

gen nmon_since_marginable=0
replace nmon_since_marginable = date2 - ym(2010,02) if vintage == 0
replace nmon_since_marginable = date2 - ym(2011,11) if vintage == 1
replace nmon_since_marginable = date2 - ym(2013,01) if vintage == 2
replace nmon_since_marginable = date2 - ym(2013,09) if vintage == 3
replace nmon_since_marginable = date2 - ym(2014,09) if vintage == 4
replace nmon_since_marginable = 0 if nmon_since_marginable < 0

keep if date2>=ym(2011,12) & date2<=ym(2015,6) 

* column (4)
eststo: qui reghdfe proportion marginable c.marginable#c.nmon_since_marginable p_float_cap p_turnover_float p_ret p_ret_12 p_bm c.nmon_since_marginable#c.p_float_cap c.nmon_since_marginable#c.p_bm c.nmon_since_marginable#c.p_turnover_float c.nmon_since_marginable#c.p_ret c.nmon_since_marginable#c.p_ret_12, absorb(fundid code date2) cluster(fundid)

* output
esttab using ../esttab/tab4.tex, b(4) se(4) ar2(3) nogaps nonotes keep(c.ii#c.nmon_since_pvin c.q_rank_t_5#c.nmon_since_pvin c.top150#c.nmon_since_pvin c.marginable#c.nmon_since_marginable) star(* 0.10 ** 0.05 *** 0.01)   replace ///
    coeflabels( ///
        "c.ii#c.nmon_since_pvin"          "Top Quintile $\times$ Months Since Last Vintage" ///
        "c.q_rank_t_5#c.nmon_since_pvin"  "Top 150 $\times$ Months Since Last Vintage" ///
        "c.top150#c.nmon_since_pvin"      "Log(Index) $\times$ Months Since Last Vintage" ///
        "c.marginable#c.nmon_since_marginable" "Marginable $\times$ Months Since Last Vintage") 




